#!/bin/bash

# sql_init
option=$1
baseDir="/buaashow/program"
curDir=`pwd`

# set env
export PATH=$PATH:$HOME/go/bin
export PATH=$PATH:/usr/local/go/bin  # for go
export GIN_MODE=release; # for gin
export GO111MODULE=on; # for go mod
export GOPROXY=https://goproxy.cn;

sql_init() {
    mysql_url=""
    mysql_port=""
    mysql_user=""
    mysql_password=""
    db_name="buaashow"

    mysql -u${mysql_user} -h${mysql_url} -P${mysql_port} -e "DROP DATABASE IF EXISTS ${db_name};"
    mysql -u${mysql_user} -h${mysql_url} -P${mysql_port} -e "CREATE DATABASE ${db_name} CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;"
} 

kill_r() {
    ps -aux | grep buaashow | awk '{print $2}' | xargs kill -9
}

tz() {
    # cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
    echo "Asia/Shanghai" >  /etc/timezone
}


kill_r
tz

if [[ "$option" == "sqlInit" ]]; then
    sql_init
fi

rm -rf $baseDir
mkdir $baseDir
cd $baseDir
# source code
git clone https://gitee.com/moglin/buaashow-gateway.git 
git clone https://gitee.com/moglin/buaashow.git 
git clone -b gh-pages https://gitee.com/moglin/buaashow-admin.git 
git clone -b gh-pages https://gitee.com/moglin/todo.git
# build
cd buaashow && go build -o buaashow && cd ../
cd buaashow-gateway && go build -o buaashowgateway && cd ../
# config file
cp $curDir/cfg-show.yaml $baseDir/buaashow/config.yaml
mkdir $baseDir/buaashow/resources
cp $curDir/cfg-gateway.yaml $baseDir/buaashow-gateway/cfg.yaml
mkdir $baseDir/buaashow-gateway/static 
mv $baseDir/buaashow-admin $baseDir/buaashow-gateway/static/admin 
mv $baseDir/todo $baseDir/buaashow-gateway/static/front
# run 
cd $baseDir/buaashow
nohup ./buaashow &
cd $baseDir/buaashow-gateway
nohup ./buaashowgateway &
